package com.zyg.apart.controller;

import com.github.pagehelper.PageInfo;
import com.zyg.apart.model.Role;
import com.zyg.apart.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Integer currentPage, Model model){
        if (currentPage == null){
            currentPage = 1;
        }
        PageInfo<Map<String,Object>> roles = roleService.getAllRole(currentPage);
        model.addAttribute("roles",roles);
        return "role/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Integer id){
        Role role = roleService.getRoleById(id);
        model.addAttribute("role",role);
        return "role/detail";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Integer id){
        Role role = roleService.getRoleById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @RequestMapping("/edit")
    public String edit(Role role){
        int i = roleService.editRole(role);
        return "forward:list";
    }

}
